<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button
          v-for="(item, index) in butData"
          :key="index"
          :type="item.color"
          @click="item.do"
          v-auth="item.auth"
        >
          <Icon :icon="item.icon" /> {{ t(item.title) }}
        </a-button>
      </template>
      <!-- 表格插槽 -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.soCode, orderStatus: 'detail' })">
          {{ record.soCode }}
        </a>
      </template>
    </BasicTable>
    <InputFormOrder @register="registerDrawer3" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderMainSaleList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { customListData, listSet } from '/@/api/test/testData';
  import { schemasData, tabConfigData } from '/@/utils/custom';
  import { BasicTable, BasicColumn, useTable, ActionItem } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import InputFormOrder from './form-order.vue';
  import { downloadByUrl2 } from '/@/utils/file/download';
  import { useGlobSetting } from '/@/hooks/setting';

  const { t } = useI18n('shop.order.mainSale');
  const { showMessage } = useMessage();
  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 120, // 表单标签宽度
    schemas: [],
    fieldMapToTime: [
      ['soMain.ddate', ['soMain.ddate_gte', 'soMain.ddate_lte']],
      ['soMain.readTime', ['soMain.readTime_gte', 'soMain.readTime_lte']],
    ],
  });
  let tableColumns = ref<any>([]);
  let butData = ref<any>([]);
  let actionColumnData = ref<ActionItem[]>([]);
  let layVoucherView = ref<any>({});
  let listTabConfig = ref<any>({});
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: listTabConfig.value.title || t('销售明细列表'),
  });
  watch(
    () => router.currentRoute.value,
    () => {
      if (router.currentRoute.value.path == '/shop/order/mainSale/list') {
        reload();
      }
    },
  );

  let title = ref('');
  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑销售订单'),
        onClick: handleForm.bind(this, { id: record.soCode, orderStatus: 'detail' }),
        auth: 'shop:order:soMain:edit',
      },
    ],
  };
  onMounted(async () => {
    setTimeout(async () => {
      const res = await listSet({ viewCode: 'shop_order_detail_list001' });
      title.value = res.layVoucherView.name;
      layVoucherView.value = res.layVoucherView;

      searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);

      tableColumns.value = layVoucherView.value.flistTabCols; //表格表头

      butData.value = layVoucherView.value.headBtns; //表格右上角按钮
      butData.value.forEach((item) => {
        if (item.btnKey === 'btnExport') {
          item.do = () => handleExport();
        }
      });

      actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮

      listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
      setProps({
        api: customListData,
        beforeFetch: (params) => {
          params.url = listTabConfig.value.api;
          return params;
        },
        autoCreateKey: listTabConfig.value.autoCreateKey,
        bordered: listTabConfig.value.bordered,
        canResize: listTabConfig.value.canResize,
        clearSelectOnPageChange: listTabConfig.value.clearSelectOnPageChange,
        clickToRowSelect: listTabConfig.value.clickToRowSelect,
        ellipsis: listTabConfig.value.ellipsis,
        emptyDataIsShowTable: listTabConfig.value.emptyDataIsShowTable,
        inset: listTabConfig.value.inset,
        isTreeTable: listTabConfig.value.isTreeTable,
        pagination: listTabConfig.value.pagination,
        showIndexColumn: listTabConfig.value.showIndexColumn,
        showSummary: listTabConfig.value.showSummary,
        showTableSetting: listTabConfig.value.showTableSetting,
        striped: listTabConfig.value.striped,
        useSearchForm: listTabConfig.value.useSearchForm,
        tableSetting: listTabConfig.value.tableSetting,
        titleHelpMessage: listTabConfig.value.titleHelpMessage,
        maxHeight: listTabConfig.value.maxHeight,
        resizeHeightOffset: listTabConfig.value.resizeHeightOffset,
      });
      if (!listTabConfig.value.immediate) {
        reload();
      }
    }, 500);
  });

  const [registerDrawer3, { openDrawer: openDrawer3 }] = useDrawer();
  const [registerTable, { reload, getForm, setProps, getSelectRows }] = useTable({
    //actionColumn: actionColumn,
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    router.push({
      path: '/shop/order/mainSale/form',
      query: {
        id: record.id,
        orderStatus: record.orderStatus,
      },
    });
  }

  // 导出
  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/shop/order/soRds/exportData',
      //target: '_self',
      params: getForm().getFieldsValue(),
    });
  }

  function handleSuccess() {
    reload();
  }
</script>
<style scoped>
  .jeesite-editable-cell__action {
    display: none;
  }

  .edit-cell-align-center {
    width: 100% !important;
  }
</style>
